<?php
include "../core/Snoopy.class.php";
include "../core/simple_html_dom.php";


$snoopy = new Snoopy;
$snoopy->fetch("http://www.techweb.com.cn/data/index.shtml");//列表
//获取并存入变量$str_html
$str_html = $snoopy->results;
//用字符串，创建一个目标文档对象 
$html=str_get_html($str_html);
//采集正文内容
foreach($html->find('div.newslist' ) as $element){
	$content = $element->innertext;
}
//构造内容对象
$html_content=str_get_html($content);

//采集链接列表
foreach($html_content->find('a' ) as $element_img){
	$link= $element_img->href;
	echo "抓取链接:".$link;
	//获取本链接的地址
	$snoopy_link = new Snoopy;
	$snoopy_link->fetch("$link");//列表
	//获取并存入变量$str_html
	$str_html_link = $snoopy_link->results;
	//根据链接创建文档
	$html_link=str_get_html($str_html_link);
	//采集正文内容
	foreach($html_link->find('div.content_txt' ) as $element_link){
		$content_link = $element_link->innertext;
		//echo "内容为：".$content_link."<hr>";
		//存入数据库
		$con = mysql_connect("localhost","root","manager");
		//echo "数据库链接为：".$con;
		if (!$con)
		{
			echo "here ……";
			die('Could not connect: ' . mysql_error());
		}
		mysql_query("set names 'utf8'",$con);
		mysql_select_db("idiaa", $con);
		$sql_link="INSERT INTO datanews (`content`)VALUES ('$content_link')";
		//echo "数据库执行代码为：".$sql_link;
		mysql_query($sql_link);
		
		mysql_close($con);
		
		/* $conn = mysqli_connect("localhost","root","manager","idiaa");
		$result = $conn->query("select * from datanews",MYSQLI_USE_RESULT);
		
		$result->free();
		$conn->close(); */
	}
	$html_link->clear();
	unset($html_link);
	//break;
}
//销毁采集对象
$html->clear();
unset($html);
?>
